import api from '../../api';
import { message } from 'antd';
import applicationModuleData from '../../routes/fileUpload/config.js';

export default {
  namespace: 'fileUploadM',
  state: {
    columns: [
      {
        key: 'tenantName',
        title: '租户',
        width: 160,
      },
      {
        key: 'uploadTime',
        title: '上传时间',
        width: 120,
      },
      {
        key: 'applicationModule',
        title: '应用模块',
        width: 120,
        render: (value, row, index) => {
          let result = applicationModuleData.filter((i) => i.value == value);
          if (result?.length > 0) {
            return result[0].title;
          } else {
            return '--';
          }
        },
      },
      {
        key: 'fileName',
        title: '文件名',
        width: 200,
        render: (value, record) => {
          return (
            <div style={{ height: record.fileList?.length > 5 && '100px', overflowY: record.fileList?.length > 5 ? 'auto' : 'none' }}>
              {record.fileList.map((val, index) => {
                return <div key={index}>{val.title}</div>;
              })}
            </div>
          );
        },
      },
      {
        key: 'operate',
        type: 'operate',
        title: '操作',
        width: 100,
      },
    ],
  },
  effects: {
    // 获取表格数据
    *getFileData({ payload, callback }, { call, put, select }) {
      const res = yield call(api.fileUpload.getFileData, payload);
      if (res?.code === 0) {
        callback(res.data);
      } else {
        callback();
        message.error(res?.msg || '查询文件列表失败，请联系管理员修复。');
        console.error(`上传文件失败，失败原因：${res?.msg}`, '接口名称：getBasicInfoData 接口：productOverView/queryJxfxBasicInfo');
      }
    },

    // 上传文件
    *uploadFile({ payload, callback }, { call, put }) {
      let res = yield call(api.fileUpload.uploadFile, payload);
      if (res && res.code == 0) {
        callback(true);
      } else {
        message.error(res?.msg || '上传文件失败，请联系管理员修复。');
        console.error(`上传文件失败，失败原因：${res?.msg}`, '接口名称：getBasicInfoData 接口：productOverView/queryJxfxBasicInfo');
      }
    },
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
